package com.icangya.admin;

import com.alibaba.fastjson.JSONObject;
import com.icangya.Utils.JSONUtils;
import com.icangya.domain.GoodsConvert;
import com.icangya.domain.GoodsConvertDetails;
import com.icangya.domain.UserWithdrawRecord;
import com.icangya.service.IGoodsService;
import com.icangya.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;


@CrossOrigin
@RestController
@RequestMapping(value = "/admin/goodOrder")
public class GoodsOrderController {


    @Autowired
    IGoodsService iGoodsService;

    /**
     * 查询代付款的订单信息
     * @param startPage   页码
     * @return
     */
    @RequestMapping(value = "getGoodsOrderPayStatus",method = RequestMethod.POST)
    public JSONObject getGoodsOrderPayStatus(int startPage){

        if(startPage < 0){
            return JSONUtils.getRoot(1, "参数有误。。", "");
        }

        try{
            List  list= iGoodsService.getGoodOrderByPayStatus(startPage);
            if(list.get(0)==null){
                return JSONUtils.getRoot(1, "暂无等待付款的订单信息。。", "");
            }
            JSONObject jsonObject=new JSONObject();
            jsonObject.put("value",list.get(0));
            jsonObject.put("totalPage",list.get(1));
            jsonObject.put("inPage",list.get(2));
            return JSONUtils.getRoot(0,"success",jsonObject);

        }catch (NullPointerException e){
            return JSONUtils.getRoot(2, "请重新登录!", null);

        }

    }

    /**
     * 根据条件查询代付款的订单信息
     * @param orderId   订单号
     * @param startTime  开始时间
     * @param endTime  结束时间
     * @param nameOrPhone  收货人姓名或手机号
     * @param address   地址
     * @param goodName  商品名称
     * @param nickName  昵称
     * @param startPage  页码
     * @return
     */
    @RequestMapping(value = "getGoodsOrderPayStatusByCondition",method = RequestMethod.POST)
    public JSONObject getGoodsOrderPayStatusByCondition(String orderId,String startTime,String endTime,String nameOrPhone,String address,String goodName,
                                                        String nickName,int startPage){

//        if(startPage < 0 || "".equals(orderId) || orderId== null || "".equals(startTime) || startTime==null || "".equals(endTime) || endTime==null || "".equals(nameOrPhone)
//                || nameOrPhone==null || "".equals(address) || address== null || "".equals(goodName) || goodName ==null || "".equals(nickName) || nickName==null){
//
//            return JSONUtils.getRoot(1, "参数有误。。", "");
//
//        }

        if(startPage < 0 ){

            return JSONUtils.getRoot(1, "参数有误。。", "");

        }

        try{
            List  list= iGoodsService.getGoodsOrderPayStatusByCondition(orderId,startTime,endTime,nameOrPhone,address,goodName,nickName,startPage);

            if(list.get(0)==null){
                return JSONUtils.getRoot(1, "暂无等待付款的订单信息。。", "");
            }
            JSONObject jsonObject=new JSONObject();
            jsonObject.put("value",list.get(0));
            jsonObject.put("totalPage",list.get(1));
            jsonObject.put("inPage",list.get(2));
            return JSONUtils.getRoot(0,"success",jsonObject);

        }catch (NullPointerException e){
            return JSONUtils.getRoot(2, "请重新登录!", null);

        }

    }


    /**
     * 根据条件查询是否收货的订单信息
     * @param orderId   订单号
     * @param startTime  开始时间
     * @param endTime  结束时间
     * @param nameOrPhone  收货人姓名或手机号
     * @param address   地址
     * @param goodName  商品名称
     * @param nickName  昵称
     * @param startPage  页码
     *@param sendStatus  收货状态
     * @return
     */
    @RequestMapping(value = "getGoodsOrderSendStatusByCondition",method = RequestMethod.POST)
    public JSONObject getGoodsOrderSendStatusByCondition(String orderId,String startTime,String endTime,String nameOrPhone,String address,String goodName,
                                                        String nickName,int startPage,int sendStatus){

//        if(  !(sendStatus==1 || sendStatus==0) || startPage < 0 || "".equals(orderId) || orderId== null || "".equals(startTime) || startTime==null || "".equals(endTime) || endTime==null || "".equals(nameOrPhone)
//                || nameOrPhone==null || "".equals(address) || address== null || "".equals(goodName) || goodName ==null || "".equals(nickName) || nickName==null){
//
//            return JSONUtils.getRoot(1, "参数有误。。", "");
//
//        }

        if(  !(sendStatus==1 || sendStatus==0) || startPage < 0 ){

            return JSONUtils.getRoot(1, "参数有误。。", "");

        }
        try{
            List  list= iGoodsService.getGoodsOrderSendStatusByCondition(orderId,startTime,endTime,nameOrPhone,address,goodName,nickName,startPage,sendStatus);

            if(list.get(0)==null){
                return JSONUtils.getRoot(1, "暂无订单信息。。", "");
            }
            JSONObject jsonObject=new JSONObject();
            jsonObject.put("value",list.get(0));
            jsonObject.put("totalPage",list.get(1));
            jsonObject.put("inPage",list.get(2));
            return JSONUtils.getRoot(0,"success",jsonObject);

        }catch (NullPointerException e){
            return JSONUtils.getRoot(2, "请重新登录!", null);

        }

    }


    /**
     * 根据发货状态查询订单信息
     * @param startPage   页码
     * @param sendStatus  发货状态
     * @return
     */
    @RequestMapping(value = "getGoodsOrderSendStatus",method = RequestMethod.POST)
    public JSONObject getGoodsOrderSendStatus(int sendStatus,int startPage){

        if(startPage < 0 || !(sendStatus==1 || sendStatus==0)) {
            return JSONUtils.getRoot(1, "参数有误。。", "");
        }

        try{
            List  list= iGoodsService.getGoodsOrderSendStatus(sendStatus,startPage);
            if(list.get(0)==null){
                return JSONUtils.getRoot(1, "订单信息。。", "");
            }
            JSONObject jsonObject=new JSONObject();
            jsonObject.put("value",list.get(0));
            jsonObject.put("totalPage",list.get(1));
            jsonObject.put("inPage",list.get(2));
            return JSONUtils.getRoot(0,"success",jsonObject);

        }catch (NullPointerException e){
            return JSONUtils.getRoot(2, "请重新登录!", null);

        }

    }








}
